package gov.nasa.anml.lifted;

import gov.nasa.anml.PDDL;
import gov.nasa.anml.State;
import gov.nasa.anml.utility.SimpleObject;

// Timed Statements know when to load, up to context (i.e. intervals are 
// evaluated in the context of the enclosing action so that all,start,end
// have the right values, i.e., so that such intervals are relative).

// Timed Statements include untimed statements, which are/include the actual
// primitive operations of the underlying state transition machine

// Applying a Timed Statement loads untimed statements into the child state
// Unfortunately it cannot immediately apply untimed statements when delay
// is unnecessary because the search finds sets of simul. actions via
// multiple steps, so that moving forward in time has to wait over multiple
// search operations.  Alternatively search states could be pairs -- the last state
// where time advanced to, and the child state being incrementally constructed
// in *that* case one could immediately apply [start] statements.

// and then there is the alternative of keeping parent pointers around indef.
// and just updating the state one is passed (so copy, and then apply transformations)
// later applied, but still simul., action can implement parallelism by following
// parent pointers [i.e., accurately evaluate [start) expression].  Prior idea
// is just this idea, but search states know the correct ancestor to walk to immediately;
// and parent pointers don't have to be kept (allowing closed list purging).

// Going back to prior idea: search states are something like (real state [no past knowledge], future state)
// with actions being applied at the time of the first state, but transforming the future state.
// forward in time then amounts to advancing the clock on 'future state' to the next
// set of discrete changes to get r', simulate them to get f', and then then the 
// search state is (r',f') -- i.e., allowing the search to swoop in and add
// simul. change with the new timepoint (the time of r').  
public interface Statement {
	// parallel change has to be non-mutex, so
	// sequential application from the template parent onto the child
	// implements parallelism without having to build memory
	// into the parent of expressions
	
	// if children have parent pointers anyways it is unnecessary to supply
	// the parent object explicitly, but does save a bit of time for the common
	// case; evaluation should never be against the child if one is to support
	// parallelism via sequential application.
	
	// In the general case, in one implementation paradigm, one would walk
	// back the whole ancestry to find old values.
	
	// if one uses special value objects for fluents, one can have fields
	// for write locks and the whole interval of the current value and
	// the prior interval, sneakily linking backwards along the ancestry (but more
	// efficient because of skipping generations).  Essentially forces
	// keeping the whole closed list, because it keeps all of the closed list's
	// data (State objects hold mostly references, and these could be lost, but
	// the referenced data would mostly not be lost).  Well, wrapping
	// collections like HashMap would also be reclaimed, as well as wrapping Entries...
	// just the bare value objects would be retained.  Perhaps that isn't so bad...
	
	// Destroys notion of State a lot though.  Fix is to forget past the earliest
	// unfinished action; earlier than that is inaccessible.  In the context
	// of sequential execution the only non-state like states are in the middle
	// of each individual action, as it should be -- in between each action
	// is a bona fide state.
	
	// need a context id 
	// return indicates whether or not c remains valid
	// (respects locks/mutexes/constraints/etc.)
	public abstract boolean apply(State p, int contextID, State c);
	//public abstract boolean start(State p, int contextID, State c);
	//public abstract boolean end(State p, int contextID, State c);
	// apply for points, start/end for intervals

	
	public abstract void translateStmt(PDDL pddl, Interval unit, PDDL.Time part);
	public void translateDecl(PDDL pddl, Interval unit);
	// more? Decomposition? Scope?
	
	// need a do() method for callbacks...or yet another heirarchy
	// or just primitive vs. compound statements?
	// compound statements generate child states with primitive statement callbacks;
	// primitive statements use themselves?
}
